package com.example.netty;

import java.util.*;
import java.util.concurrent.TimeUnit;


import com.example.util.ConvertCode;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.Channel;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import io.netty.channel.ChannelFuture;
@Component
public class TcpJob {




    /**
     * 通过netty客户端定时向所有长连接的设备请求数据
     * 每1S获取一次,首次延迟10s执行
     */
    @Scheduled(initialDelay = 5, fixedRate = 5, timeUnit = TimeUnit.SECONDS)
    private void instantaneousFlow() {
        try {
            for (Channel channel:ChannelMap.getChannelMap().values()) {
                System.out.println("channel = " +channel);
                //Netty需要用ByteBuf传输
                ByteBuf bufff = Unpooled.buffer();
                //对接需要16进制
                bufff.writeBytes(ConvertCode.hexString2Bytes("1103006000018684"));
                channel.writeAndFlush(bufff);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
